//
//  HomeViewController.swift
//  SwiftProject
//
//  Created by wwj on 16/2/23.
//  Copyright © 2016年 wwj. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {
    
    var collectionView :UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        addCollection()
        addRefresh()
    }
    
    //MARK: 添加Collection
    private func addCollection(){
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 8
        layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10)
        collectionView = UICollectionView(frame: CGRectMake(0, 0, SCREEN_WITH, SCREEN_HEIGHT), collectionViewLayout: layout)
        
        collectionView.registerNib(UINib(nibName: "HomeCell", bundle: nil), forCellWithReuseIdentifier: "HomeCell")
        
        collectionView.registerClass(HomeHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HomeHeaderView")
        
        collectionView.registerNib(UINib(nibName: "HomeFooterView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "HomeFooterView")
        
        collectionView.backgroundColor = UIColor.orangeColor()
        collectionView.delegate = self
        collectionView.dataSource = self
        self.view .addSubview(collectionView)
    }
    
    //MARK:添加刷新控件
    private func addRefresh(){
        collectionView.mj_header = RefreshHeader(refreshingBlock: { () -> Void in
            self.collectionView.mj_header.endRefreshing()
        })
    }
}


//MARK: Collection代理和数据源方法
extension HomeViewController:UICollectionViewDelegate, UICollectionViewDataSource{
    //MARK: Cell高度
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        if(indexPath.section == 0){
            return CGSizeMake(SCREEN_WITH - 20, 140)
        }else{
            return CGSizeMake((SCREEN_WITH - 20) * 0.5 - 4, 250)
        }
    }
    
    //MARK: 头部高度
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 0 {
            return CGSizeMake(SCREEN_WITH, 195)
        }else{
            return CGSizeMake(SCREEN_WITH, 20)
        }
        
    }
    
    //MARK:底部高度
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        if section == 0 {
            return CGSizeMake(SCREEN_WITH, 10)
        } else{
            return CGSizeMake(SCREEN_WITH, 50)
        }
    }
    
    //MARK: 头部和底部View
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
        if indexPath.section == 0 {
            let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "HomeHeaderView", forIndexPath: indexPath) as! HomeHeaderView
            return headerView
        }else{
            let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "HomeFooterView", forIndexPath: indexPath) as! HomeFooterView
            return footerView
        }
    }
    
    //MARK: Cell数目
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("HomeCell", forIndexPath: indexPath) as! HomeCell
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
    }
}
 